package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.feign.constants.MQConstants;
import com.hmall.feign.dto.Item;
import com.hmall.feign.dto.PageDTO;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //分页查询商品
    @GetMapping("/list")
    public PageDTO<Item> itemList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "1") Integer size
    ) {
        Page<Item> result = itemService.page(new Page<>(page, size));
        return new PageDTO<Item>(result.getTotal(), result.getRecords());
    }

    //根据id查询商品
    @GetMapping("/{id}")
    public Item getItemById(@PathVariable("id") Long id) {
        return itemService.getById(id);
    }

    //新增商品
    @PostMapping
    public void insert(@RequestBody Item item) {
        itemService.save(item);
        rabbitTemplate.convertAndSend(MQConstants.ITEM_EXCHANGE, MQConstants.ITEM_INSERT_KEY, item.getId());
    }

    /**
     * 减少商品库存
     *
     * @param itemId 商品id
     * @param num    要减少的库存数量
     */
    @PostMapping("/reduceItemStockNum")
    public void reduceItemStockNum(Long itemId, Integer num) {
        itemService.reduceItemStockNum(itemId, num);
        rabbitTemplate.convertAndSend(MQConstants.ITEM_EXCHANGE, MQConstants.ITEM_INSERT_KEY, itemId);
    }

    /**
     * 上下架
     *
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    private void upDown(@PathVariable Long id, @PathVariable Integer status) {
        itemService.upDown(id, status);
        rabbitTemplate.convertAndSend(MQConstants.ITEM_EXCHANGE, MQConstants.ITEM_INSERT_KEY, id);
    }

    /**
     * 修改
     *
     * @param item
     */
    @PutMapping
    private void editItem(@RequestBody Item item) {
        itemService.editItem(item);
        rabbitTemplate.convertAndSend(MQConstants.ITEM_EXCHANGE, MQConstants.ITEM_INSERT_KEY, item.getId());
    }


    /**
     * 删除
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    private void delItem(@PathVariable Long id) {
        itemService.delItem(id);
        rabbitTemplate.convertAndSend(MQConstants.ITEM_EXCHANGE, MQConstants.ITEM_DELETE_KEY, id);
    }


    /**
     * 增加商品库存
     *
     * @param itemId
     * @param num
     */
    @PutMapping("/returnItemStockNum/{itemId}/{num}")
    private void returnItemStockNum(@PathVariable Long itemId, @PathVariable Integer num) {
        itemService.returnItemStockNum(itemId, num);
        rabbitTemplate.convertAndSend(MQConstants.ITEM_EXCHANGE, MQConstants.ITEM_INSERT_KEY, itemId);
    }

    /**
     * 求总条数
     *
     * @return
     */
    @GetMapping("/count")
    public int count() {
        return itemService.count();
    }


}
